package com.kedll.library.mvp;

import android.databinding.BaseObservable;
import android.util.Log;

import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.disposables.Disposable;

/**
 * @author QiQi
 * @date 2017/4/20
 */

public class BasePresenter<T> extends BaseObservable {

    protected T mView;
    private CompositeDisposable mCompositeSubscription;

    public BasePresenter(T view) {
        onAttach(view);
    }

    public BasePresenter() {

    }

    public void onAttach(T view) {
        this.mView = view;
    }

    public void onDetach() {
        mView = null;
        onUnsubscribe();
    }

    private void onUnsubscribe() {
        if (mCompositeSubscription != null && !mCompositeSubscription.isDisposed()) {
            Log.e("presenter", "RxJava 取消注册" + getClass());
            mCompositeSubscription.dispose();
            mCompositeSubscription = null;

        }
    }

    public void addSubscription(Disposable disposable) {
        if (mCompositeSubscription == null) {
            mCompositeSubscription = new CompositeDisposable();
        }
        mCompositeSubscription.add(disposable);
    }
}
